package com.aia.prm.web.ria.app;

import java.io.Serializable;

import org.apache.log4j.Logger;

import com.aia.prm.web.ria.app.ui.G2View;
import com.aia.prm.web.ria.app.ui.config.ConfigView;
import com.aia.prm.web.ria.app.ui.gpinsu.GpinsuView;
import com.cfh.util.Tools;
import com.vaadin.ui.UriFragmentUtility.FragmentChangedEvent;
import com.vaadin.ui.UriFragmentUtility.FragmentChangedListener;

@SuppressWarnings("serial")
public class MyFragmentChangedListener implements FragmentChangedListener, Serializable {
	private static final Logger logger = Logger.getLogger(MyFragmentChangedListener.class);
	private boolean isFragmentDebugAble = true;
	private final static String FRAG_PRE = "debug/module/";

	PrmApplication prmApplication = null;

	MyFragmentChangedListener(PrmApplication arg) {
		prmApplication = arg;
		if ("Y".equalsIgnoreCase(Tools.getProperties("console.debug"))) {
			logger.debug("console.debug=Y");
			isFragmentDebugAble = true;
		} else {
			logger.debug("console.debug!=Y");
			isFragmentDebugAble = false;
		}
	}

	public void fragmentChanged(FragmentChangedEvent source) {
		String fragment = source.getUriFragmentUtility().getFragment();
		if (isFragmentDebugAble && fragment.startsWith(FRAG_PRE)) {
			logger.debug("************** falstLogin ************** fragment=" + fragment);
			prmApplication.falstLogin();
			int pos = fragment.indexOf(FRAG_PRE);
			String moduleName = fragment.substring(pos + FRAG_PRE.length());
			if ("config".equalsIgnoreCase(moduleName)) {
				logger.debug("************** falstLogin ************** moduleName=" + moduleName);
				G2View view = (G2View) prmApplication.getMainWindow().getContent();
				view.addTab(new ConfigView(prmApplication), "参数配置");
			}

			else if ("gpinsu".equalsIgnoreCase(moduleName)) {
				logger.debug("************** falstLogin ************** moduleName=" + moduleName);
				G2View view = (G2View) prmApplication.getMainWindow().getContent();
				view.addTab(new GpinsuView(prmApplication), "团险产品管理");
			}
			// else if ("qnpe".equalsIgnoreCase(moduleName)) {
			// logger.debug("************** falstLogin ************** moduleName="
			// + moduleName);
			// G2View view = (G2View)
			// prmApplication.getMainWindow().getContent();
			// view.addTab(new QnpeView(prmApplication), "NMP项目报表");
			// }
		}

	}

}
